package com.sdhs.projectschedule.service.impl;

import com.sdhs.common.utils.DateUtils;
import com.sdhs.common.utils.SecurityUtils;
import com.sdhs.projectschedule.domain.GcProgressPaymentCollectionFill;
import com.sdhs.projectschedule.domain.GcProgressPaymentCollectionTracking;
import com.sdhs.projectschedule.mapper.GcProgressPaymentCollectionFillMapper;
import com.sdhs.projectschedule.mapper.GcProgressPaymentCollectionTrackingMapper;
import com.sdhs.projectschedule.service.IGcProgressPaymentCollectionTrackingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;


/**
 * 项目进度控制--回款跟踪Service业务层处理
 *
 * @author andong
 * @date 2025-03-12
 */
@Service
public class GcProgressPaymentCollectionTrackingServiceImpl implements IGcProgressPaymentCollectionTrackingService {

    @Autowired
    private GcProgressPaymentCollectionTrackingMapper gcProgressPaymentCollectionTrackingMapper;
    @Autowired
   private GcProgressPaymentCollectionFillMapper progressPaymentCollectionFillMapper;
    /**
     * 查询项目进度控制--回款跟踪
     *
     * @param id 项目进度控制--回款跟踪主键
     * @return 项目进度控制--回款跟踪
     */
    @Override
    public GcProgressPaymentCollectionTracking selectGcProgressPaymentCollectionTrackingById(Long id) {
        return gcProgressPaymentCollectionTrackingMapper.selectGcProgressPaymentCollectionTrackingById(id);
    }
    /**
     * 查询项目进度控制--回款跟踪-合同信息
     *
     * @param id 项目进度控制--项目id
     */
    @Override
    public GcProgressPaymentCollectionTracking selectContractByProjectId(Long id,String contractId) {
        return gcProgressPaymentCollectionTrackingMapper.selectContractByProjectId(id,contractId);
    }

    @Override
    public List<GcProgressPaymentCollectionTracking> selectContractsByProjectId(Long id) {
           return gcProgressPaymentCollectionTrackingMapper.selectContractsByProjectId(id);
    }

    /**
     * 查询项目进度控制--回款跟踪列表
     *
     * @param gcProgressPaymentCollectionTracking 项目进度控制--回款跟踪
     * @return 项目进度控制--回款跟踪
     */
    @Override
    public List<GcProgressPaymentCollectionTracking> selectGcProgressPaymentCollectionTrackingList(GcProgressPaymentCollectionTracking gcProgressPaymentCollectionTracking) {

        List<GcProgressPaymentCollectionTracking> list = gcProgressPaymentCollectionTrackingMapper.selectGcProgressPaymentCollectionTrackingList(gcProgressPaymentCollectionTracking);

        GcProgressPaymentCollectionFill gcProgressPaymentCollectionFill = progressPaymentCollectionFillMapper.selectGcProgressPaymentCollectionFillSum(gcProgressPaymentCollectionTracking.getProjectId(),gcProgressPaymentCollectionTracking.getContractId());

        // 确保对象不为 null，并设置默认值
        if (gcProgressPaymentCollectionFill == null) {
            gcProgressPaymentCollectionFill = new GcProgressPaymentCollectionFill();
            gcProgressPaymentCollectionFill.setPaidBack(BigDecimal.ZERO);
            gcProgressPaymentCollectionFill.setInvoicedOut(BigDecimal.ZERO);
            gcProgressPaymentCollectionFill.setPaidBackProportion(BigDecimal.ZERO);
            gcProgressPaymentCollectionFill.setInvoicedOutProportion(BigDecimal.ZERO);
        } else {
            // 如果对象不为 null，检查字段是否为 null
            if (gcProgressPaymentCollectionFill.getPaidBack() == null) {
                gcProgressPaymentCollectionFill.setPaidBack(BigDecimal.ZERO);
            }
            if (gcProgressPaymentCollectionFill.getInvoicedOut() == null) {
                gcProgressPaymentCollectionFill.setInvoicedOut(BigDecimal.ZERO);
            }
            if (gcProgressPaymentCollectionFill.getPaidBackProportion() == null) {
                gcProgressPaymentCollectionFill.setPaidBackProportion(BigDecimal.ZERO);
            }
            if (gcProgressPaymentCollectionFill.getInvoicedOutProportion() == null) {
                gcProgressPaymentCollectionFill.setInvoicedOutProportion(BigDecimal.ZERO);
            }
        }
        // 如果都为空的话直接返回集合
        if (gcProgressPaymentCollectionFill.getPaidBack().equals(BigDecimal.ZERO) && gcProgressPaymentCollectionFill.getInvoicedOut().equals(BigDecimal.ZERO) &&
                gcProgressPaymentCollectionFill.getPaidBackProportion().equals(BigDecimal.ZERO) && gcProgressPaymentCollectionFill.getInvoicedOutProportion().equals(BigDecimal.ZERO)) {
            return list;
        }
        //回款金额
        BigDecimal remaining = gcProgressPaymentCollectionFill.getPaidBack();
        BigDecimal cumulativeSum = BigDecimal.ZERO;

        //开票金额
        BigDecimal invoicedRemaining = gcProgressPaymentCollectionFill.getInvoicedOut();
        BigDecimal invoicedCumulativeSum = BigDecimal.ZERO;

        //回款比例
        BigDecimal backProportionRemaining = gcProgressPaymentCollectionFill.getPaidBackProportion();
        BigDecimal backProportionSum = BigDecimal.ZERO;

        //开票比例
        BigDecimal invoicedOutProportionRemaining = gcProgressPaymentCollectionFill.getInvoicedOutProportion();
        BigDecimal invoicedOutProportionSum = BigDecimal.ZERO;

        for (int i = 0; i < list.size(); i++) {
            GcProgressPaymentCollectionTracking tracking = list.get(i);
            //回款金额
            BigDecimal paidBackMoney = tracking.getPaidBackMoney();
            if(paidBackMoney == null) paidBackMoney = BigDecimal.ZERO;
            cumulativeSum = cumulativeSum.add(paidBackMoney);
            //回款金额
            if (remaining.compareTo(cumulativeSum) >= 0) {
                tracking.setPaidBack(paidBackMoney);
            } else {
                BigDecimal previousCumulativeSum = cumulativeSum.subtract(paidBackMoney);
                BigDecimal paidBack = remaining.subtract(previousCumulativeSum);
                paidBack=paidBack.max(BigDecimal.ZERO);
                tracking.setPaidBack(paidBack);
                // 剩余元素的 paidBack 设为 0
                for (int j = i + 1; j < list.size(); j++) {
                    list.get(j).setPaidBack(BigDecimal.ZERO);
                }
            }

            //开票金额
            BigDecimal invoicedOut = tracking.getPaidBackMoney();
            if(invoicedOut == null) invoicedOut = BigDecimal.ZERO;
            invoicedCumulativeSum=invoicedCumulativeSum.add(invoicedOut);
            //开票金额
            if(invoicedRemaining.compareTo(invoicedCumulativeSum)>=0){
                tracking.setInvoicedOut(invoicedOut);
            }else {
                BigDecimal subtract = invoicedCumulativeSum.subtract(invoicedOut);
                BigDecimal subtract1 = invoicedRemaining.subtract(subtract);
                subtract1=subtract1.max(BigDecimal.ZERO);
                tracking.setInvoicedOut(subtract1);
                // 剩余元素的 paidBack 设为 0
                for (int k = i + 1; k< list.size(); k++) {
                    list.get(k).setInvoicedOut(BigDecimal.ZERO);
                }
            }

            //回款比例
            BigDecimal PaidBackProportion = tracking.getPlannedRepaymentProportion();
            if(PaidBackProportion == null) PaidBackProportion = BigDecimal.ZERO;
            backProportionSum=backProportionSum.add(PaidBackProportion);

            //回款比例
            if(backProportionRemaining.compareTo(backProportionSum)>=0){
                tracking.setPaidBackProportion(PaidBackProportion);
            }else {
                BigDecimal paidBackProportion = backProportionSum.subtract(PaidBackProportion);
                BigDecimal paidBackProportion1 = backProportionRemaining.subtract(paidBackProportion);
                paidBackProportion1=paidBackProportion1.max(BigDecimal.ZERO);
                tracking.setPaidBackProportion(paidBackProportion1);
                // 剩余元素的 PaidBackProportion 设为 0
                for (int k = i + 1; k< list.size(); k++) {
                    list.get(k).setPaidBackProportion(BigDecimal.ZERO);
                }
            }

            //开票比例invoiced_out_proportion
            BigDecimal invoicedOutProportion = tracking.getPlannedRepaymentProportion();
            if(invoicedOutProportion == null) invoicedOutProportion = BigDecimal.ZERO;
            invoicedOutProportionSum=invoicedOutProportionSum.add(invoicedOutProportion);

            //开票比例
            if(invoicedOutProportionRemaining.compareTo(invoicedOutProportionSum)>=0){
                tracking.setInvoicedOutProportion(invoicedOutProportion);
            }else {
                BigDecimal invoicedOutProport = invoicedOutProportionSum.subtract(invoicedOutProportion);
                BigDecimal invoicedOutProport1 = invoicedOutProportionRemaining.subtract(invoicedOutProport);
                invoicedOutProport1=invoicedOutProport1.max(BigDecimal.ZERO);
                tracking.setInvoicedOutProportion(invoicedOutProport1);
                // 剩余元素的 PaidBackProportion 设为 0
                for (int k = i + 1; k< list.size(); k++) {
                    list.get(k).setInvoicedOutProportion(BigDecimal.ZERO);
                }
            }
        }

        return list;
    }

    /**
     * 新增项目进度控制--回款跟踪
     *
     * @param gcProgressPaymentCollectionTracking 项目进度控制--回款跟踪
     * @return 结果
     */
    @Override
    public int insertGcProgressPaymentCollectionTracking(GcProgressPaymentCollectionTracking gcProgressPaymentCollectionTracking) {
        gcProgressPaymentCollectionTracking.setCreater(SecurityUtils.getUsername());
        gcProgressPaymentCollectionTracking.setCreateTime(DateUtils.getNowDate());
        return gcProgressPaymentCollectionTrackingMapper.insertGcProgressPaymentCollectionTracking(gcProgressPaymentCollectionTracking);
    }

    /**
     * 修改项目进度控制--回款跟踪
     *
     * @param gcProgressPaymentCollectionTracking 项目进度控制--回款跟踪
     * @return 结果
     */
    @Override
    public int updateGcProgressPaymentCollectionTracking(GcProgressPaymentCollectionTracking gcProgressPaymentCollectionTracking) {
        gcProgressPaymentCollectionTracking.setUpdater(SecurityUtils.getUsername());
        gcProgressPaymentCollectionTracking.setUpdateTime(DateUtils.getNowDate());
        return gcProgressPaymentCollectionTrackingMapper.updateGcProgressPaymentCollectionTracking(gcProgressPaymentCollectionTracking);
    }

    /**
     * 批量删除项目进度控制--回款跟踪
     *
     * @param ids 需要删除的项目进度控制--回款跟踪主键
     * @return 结果
     */
    @Override
    public int deleteGcProgressPaymentCollectionTrackingByIds(Long[] ids) {
        return gcProgressPaymentCollectionTrackingMapper.deleteGcProgressPaymentCollectionTrackingByIds(ids);
    }

    /**
     * 删除项目进度控制--回款跟踪信息
     *
     * @param id 项目进度控制--回款跟踪主键
     * @return 结果
     */
    @Override
    public int deleteGcProgressPaymentCollectionTrackingById(Long id) {
        return gcProgressPaymentCollectionTrackingMapper.deleteGcProgressPaymentCollectionTrackingById(id);
    }
}
